home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl5
- # Creates buildable SGML source from parent SGML file and components.
- # Called by commondocrules for "make book.full" operation.
- #
- # Daniel R. Dodge Wed Oct 28 17:08:30 CST 1998
- # originally written as "build-dweb-post" by Howard E. Pierce
- #
- # Requires perl5 to run correctly.
-
- ################################################################################
- ### Define routine to handle various system signals and the signals that
- ### activate it:
-
- sub SigHandler
- {
- my ($Sig) = @_ ;
-
- if ("$SavEbtrc" ne '')
- {
- $ENV{'EBTRC'} = $SavEbtrc ;
- }
- chdir "$Pwd" ;
- if ($OMask)
- {
- umask ($OMask) ;
- }
- ## DRD system ("/usr/bin/rm -rf $MkbookTmpDir $BookListLock") ;
- print STDERR "\n\a$Name quits because of '$Sig' signal.\n" ;
- exit 9 ;
- }
-
- $SIG{'ABRT'} = 'SigHandler' ; $SIG{'FPE'} = 'SigHandler' ;
- $SIG{'FREEZE'} = 'SigHandler' ; $SIG{'HUP'} = 'SigHandler' ;
- $SIG{'ILL'} = 'SigHandler' ; $SIG{'INT'} = 'SigHandler' ; ### Ctrl-C
- $SIG{'KILL'} = 'SigHandler' ; $SIG{'QUIT'} = 'SigHandler' ;
- $SIG{'SEGV'} = 'SigHandler' ; $SIG{'STOP'} = 'SigHandler' ;
- $SIG{'TERM'} = 'SigHandler' ; $SIG{'ALRM'} = 'SigHandler' ;
- $SIG{'BUS'} = 'SigHandler' ; $SIG{'CONT'} = 'SigHandler' ;
- $SIG{'EMT'} = 'SigHandler' ; $SIG{'LWP'} = 'SigHandler' ;
- $SIG{'PIPE'} = 'SigHandler' ; $SIG{'POLL'} = 'SigHandler' ;
- $SIG{'PROF'} = 'SigHandler' ; $SIG{'PWR'} = 'SigHandler' ;
- $SIG{'RTMAX'} = 'SigHandler' ; $SIG{'RTMAX-1'} = 'SigHandler' ;
- $SIG{'RTMAX-2'} = 'SigHandler' ; $SIG{'RTMAX-3'} = 'SigHandler' ;
- $SIG{'RTMIN'} = 'SigHandler' ; $SIG{'RTMIN+1'} = 'SigHandler' ;
- $SIG{'RTMIN+2'} = 'SigHandler' ; $SIG{'RTMIN+3'} = 'SigHandler' ;
- $SIG{'SYS'} = 'SigHandler' ; $SIG{'THAW'} = 'SigHandler' ;
- $SIG{'TRAP'} = 'SigHandler' ; $SIG{'TSTP'} = 'SigHandler' ;
- $SIG{'TTIN'} = 'SigHandler' ; $SIG{'TTOU'} = 'SigHandler' ;
- $SIG{'URG'} = 'SigHandler' ; $SIG{'USR1'} = 'SigHandler' ;
- $SIG{'USR2'} = 'SigHandler' ; $SIG{'VTALRM'} = 'SigHandler' ;
- $SIG{'WAITING'} = 'SigHandler' ; $SIG{'XCPU'} = 'SigHandler' ;
- $SIG{'XFSZ'} = 'SigHandler' ;
-
-
- ################################################################################
- ### Waits for user input to exit
- ### $1 = Numeric exit code
- ### $2 = InterActive flag
-
- sub WaitAndExit
- {
- if ("$SavEbtrc" ne '')
- {
- $ENV{'EBTRC'} = $SavEbtrc ;
- }
-
- chdir "$Pwd" ;
- if ($OMask)
- {
- umask ($OMask) ;
- }
-
- if ($_[1])
- {
- print "\nPress RETURN to continue: " ;
- $NoOp = <STDIN> ;
- }
-
- exit $_[0] ;
- }
-
-
- ################################################################################
- ### Subroutine to run system command lines, check for errors, & issue messages
- ### $1 - $n = The system command lines to run
-
- sub RunIt
- {
- my ($CmdLine, $ECode) ;
-
- foreach $CmdLine (@_)
- {
- $ECode = system ("$CmdLine") ;
- if ($ECode)
- {
- if ($ECode > 255) { $ECode /= 256 ; }
- print STDERR "\a\n$Name: $ECode from:\n\t$CmdLine\n" ;
- &WaitAndExit ("$ECode", "$InterActive")
- }
- }
- }
-
-
-
- ################################################################################
- ### Replaces hard returns in nonwrapping elements with the <newline> element
- ### This is used only for InSight/DynaWeb processing of buildable source
- ### $1 = String containing the name of the SGML file
- ### $2 = String containing one complete SGML file
-
- sub DoNewLines {
- my ($FileName, $Buff) = @_ ;
- my ($Start, $Tag, $Stop, $ToChange, $String) ;
-
- LOOP4:
- while ($$Buff =~ m<(literallayout|programlisting|screen|synopsis[^>]+format="yes")[\s>]gis) {
- $Start = pos ($$Buff) ;
- $Tag = $1 ;
- $Tag =~ s\s.*$s ;
- $Stop = index ($$Buff, "</$Tag>", $Start) ;
- if ($Stop < 0) {
- print STDERR "\n\a<$Tag> missing end tag in '$FileName'!\n" ;
- next LOOP4 ;
- }
- ### -- and ++ so that <newline> NOT added right after/b4 start/end tags
- $ToChange = --$Stop - ++$Start ;
- if ($ToChange > 0) { ### Ignore any empty tag pairs
- $String = substr ($$Buff, $Start, $ToChange) ;
- if ($String =~ s *\n<newline>gs) { ### Remove spaces at EOLN
- substr ($$Buff, $Start, $ToChange) = $String ;
- $Stop += length ($String) - $ToChange ;
- } }
- $Stop += length ("</$Tag>") ;
- pos ($$Buff) = $Stop ;
- } }
-
-
-
- ################################################################################
- ### Cleans up SGML files: Removes Adept processing instructions and joins
- ### tags broken across lines
- ### This contains both general SGML cleanup and InSight/Dynaweb tweaks
- ### $1 = Name of file
- ### $2 = String containing one complete SGML file
-
- sub CleanUp {
- my ($FileName, $Buff) = @_ ;
- my (@Tmp, $Ent, %Tmp, @Ents, $Tmp) ;
-
-
- ### General cleanup ######################################################
-
- ### change the \r\n output from spam into single carriage-returns
- $$Buff =~ s\r\r\n\ng ; ## sometimes two \r passthru by spam
- $$Buff =~ s\r\n\ng ;
- $$Buff =~ s\rg ;
-
- ### Join line-broken tags:
- ## SGML allows this: <section
- ## id="thing1"
- ## >
- ## But processing in scripts simplified if < to > on one line.
- while ($$Buff =~ s(<[^>]+)[\n]$1 g)
- { $NoOp = 1 ; }
-
- ### Delete all SGML-standard comments:
- $$Buff =~ s<!--.*?-->gs ;
- $$Buff =~ s<!>gs ;
-
- ### Replace DynaWeb packaging "collections" contents with empty tags:
- ### This can be removed once the <collections> tag is no longer supported
- $$Buff =~ s<collections.*?</collections><collections></collections>gis ;
-
- ### End of general cleanup ###############################################
-
-
- ### InSight-specific changes #############################################
-
- ### Change all carriage-return processing instructions to newline elements:
- $$Buff =~ s<\?Pub\s*_(newline>)<$1g ;
-
- ### Remove Adept processing instructions:
- $$Buff =~ s\n<\?[^>]+>\n\ng ;
- $$Buff =~ s<\?[^>]+>g ;
-
- ### Fix <stuff> problem for mkbook parser:
- ## first change all <sgmltag> to SGML_TAGOsgmltagSGML_TAGC :
- $$Buff =~ s<([^>]*)>SGML_TAGO$1SGML_TAGCgs ;
- ## change leftover > chars to > :
- $$Buff =~ s>>gs ;
- ## change delimiters back:
- $$Buff =~ sSGML_TAGO<gs ;
- $$Buff =~ sSGML_TAGC>gs ;
-
- ### Change certain character entities to literals (stylesheet bugs):
- $$Buff =~ s–-gs ;
- $$Buff =~ s—--gs ;
- $$Buff =~ s gs ;
- $$Buff =~ s…...gs ;
-
- ### Change entityref values to include .gif extension:
- ## Might have <graphic entityref="abc" magnification="90"></graphic>
- ## or have <graphic magnification="90" entityref="abc"></graphic>
- $$Buff =~ s<graphic([^>]*)entityref\s*=\s*\"([^"]*)\"<graphic$1entityref=\"$2.gif\"igs ;
-
- ### Replace hard returns in nonwrapping elements with the <newline> element:
- &DoNewLines ($FileName, $Buff) ;
-
- ### End of InSight-specific changes ######################################
- }
-
-
-
- ################################################################################
- ### Returns content of a tag pair
- ### $1 = Array index at which to start looking
- ### $2 = tag name (SGML requires case-insensitive match)
- ### $3 = Array in which to search
-
- sub GetTagContent {
- my ($I, $Tag, $Buff) = @_ ;
- my ($J, $Content, ) ;
-
- while ( ($$I <= $#$Buff) && ($$Buff[$$I] !~ m<$$Tag\bi) )
- {
- $$I++ ;
- }
- $J = $$I ;
- while ( ($J <= $#$Buff) && ($$Buff[$J] !~ m</$$Tag\bi) )
- {
- $J++ ;
- }
- if ($$I == $J)
- {
- $Content = $$Buff[$$I] ;
- }
- else
- {
- $Content = join (' ', @$Buff[$$I..$J]) ;
- }
- $Content =~ s/\n/ /g ;
- $Content =~ s^.*<$$Tag[^>]*>(.*)</$$Tag.*$1i ;
- $Content =~ s/ +/ /g ;
- $Content =~ s/^ +// ;
- $Content =~ s/ +$// ;
-
- return ($Content) ;
- }
-
-
- ################################################################################
- ### Returns value for an attribute of an element
- ### $1 = Array index at which to start looking
- ### $2 = tag name (SGML requires case-insensitive match)
- ### $3 = attribute name (SGML requires case-insensitive match)
- ### $4 = Array in which to search
-
- sub GetAttValue {
- my ($I, $Tag, $AttribName, $Buff) = @_ ;
- my ($J, $TagContent, $AttValue) ;
-
- ### Find the element (tag might be lowercase or uppercase)
- while ( ($$I <= $#$Buff) && ($$Buff[$$I] !~ m<$$Tag\bi) )
- {
- $$I++ ;
- }
- $J = $$I ;
- while ( ($J <= $#$Buff) && ($$Buff[$J] !~ m</$$Tag\bi) )
- {
- $J++ ;
- }
- if ($$I == $J)
- {
- $TagContent = $$Buff[$$I] ;
- }
- else
- {
- $TagContent = join (' ', @$Buff[$$I..$J]) ;
- }
-
- $TagContent =~ s/\n/ /g ;
- $TagContent =~ s^.*<$$Tag([^>]*)>.*$1i ;
- ($AttValue = $TagContent) =~ s^.*\s$$AttribName\s*=\s*\"([^"]*)\".*$1i ;
-
- return ($AttValue) ;
-
- }
-
-
- ################################################################################
- ### Gets information from book SGML file
- ### $1 = Book name; returned by this routine
- ### $2 = Complete publication number; returned by this routine
- ### $3 = Book's version; returned by this routine
- ### $4 = Book's Title; returned by this routine
- ### $5 = Book's shorttitle attribute; returned by this routine
- ### $6 = Book's bookshelf attribute; returned by this routine
- ### $7 = Array of lines from text of book file
-
- sub GetBookInfo {
- my ($BookName, $PartNo, $Version, $Title, $ShortTitle,
- $BookShelf, $BuffSGML) = @_ ;
- my ($ClassCode, $Base, $Answer, $I, $AttribName, $Tag,
- $PartInfo, $Tmp, @Tmp, $ChapSuffix) ;
-
- undef $$BookName ;
- undef $$PartNo ;
- undef $ClassCode ;
- undef $Base ;
- undef $$Version ;
-
- undef $$ShortTitle ;
- undef $$BookShelf ;
-
-
- ## DRD Future version:
- ## Need to check value in the SGML file against the one in the Makefile. If not
- ## the same, ask user if the value in the Makefile should be used.
- ## Default = yes (need to be able to use Makefile to override values in the SGML file.
-
- if (grep (/<partnumber\b/i, @$BuffSGML))
- {
- $I = 0 ;
- $Tag = 'partnumber' ;
- $PartInfo = &GetTagContent (\$I, \$Tag, $BuffSGML) ;
-
- $I = 0 ;
- $Tag = 'partnumber' ;
- $AttribName = 'shorttitle' ;
- $$ShortTitle = &GetAttValue (\$I, \$Tag, \$AttribName, $BuffSGML) ;
-
- ($ClassCode = $PartInfo) =~ s^.*<classcode[^>]*>(.*)</classcode>.*$\1i ;
- ($Base = $PartInfo) =~ s^.*<base[^>]*>(.*)</base>.*$\1i ;
- ($$Version = $PartInfo) =~ s^.*<version[^>]*>(.*)</version>.*$\1i ;
- $$PartNo = "${ClassCode}-${Base}-$$Version" ;
- }
- else
- {
- print "\n$Name: Only for processing entire book! \n" ;
- &WaitAndExit ("1", "$InterActive") ;
- }
-
- ## Get "title" (full title)
- $I = 0 ;
- $Tag = 'title' ;
- $$Title = &GetTagContent (\$I, \$Tag, $BuffSGML) ;
- $$Title =~ s/\&[^;]+;//g ; ### Remove any character entities
-
- $$BookName = $$PartNo ;
- }
-
-
-
-
- ################################################################################
- ##### MAIN #####
- ################################################################################
-
-
- $[ = 0 ;
- ($Name = $0) =~ s^.*/ ;
- select((select(STDOUT), $| = 1)[0]) ; ### No buffering of stdout for messages
- $Usage = "
- Usage: $Name [-draft] [-numheads] -I<inputfile> -O<outputfile>
-
- -draft
- Draft version; keep the comments and revision indicators
- for reviewers in displayed version (InSight or the
- 'post4review' DynaWeb copies).
-
- -numheads
- Inserts the numheads=Y attribute on the <sgidocbk>
- tag for use by the stylesheets
-
- <inputfile>
- Name of the .tmp SGML file generated by 'spam' in the
- current book's working directory.
-
- <outputfile>
- Name of the final buildable source file, by convention
- the <shorttitle>.sgml name.
-
- Example:
-
- $Name -draft -IFSafe_IG.sgml.tmp -OFSafe_IG.sgml
- " ;
-
-
- chop ($Pwd = `/usr/bin/pwd`) ;
- $SgmlDocPath = "$Pwd" ; ## DRD revised this variable
-
- ## definitions
- $ToolRoot = $ENV{'TOOLROOT'} ;
- $DraftVersion = 0 ; ## flag for draft or final (default is final; strip <comment> elements)
-
- $Indexer = "$ToolRoot/usr/share/Insight/bin/indexgen_sgidocbk" ;
- $XformSGML_public = "$ToolRoot/usr/share/Insight/bin/xformsgml_public" ;
-
- $TmpFile = "tmp_make_bldsgml.$$" ; ## temporary file(s) created and deleted
- ## by this script
-
- $InterActive = 0 ; ## set to 1 if using from Adept-spawned shell window
- $NumberedHeads = 0;
-
- if (! @ARGV) {
- print STDERR "$Usage" ;
- &WaitAndExit ("1", "$InterActive") ;
- }
-
- while (@ARGV) {
- $Arg = shift (@ARGV) ;
- if ("$Arg" eq '-draft')
- {
- $DraftVersion = 1 ;
- }
- elsif ($Arg eq '-numheads')
- {
- $NumberedHeads = 1;
- }
- elsif ($Arg =~ /^-I.+/)
- {
- $Arg =~ s/^-I// ;
- push (@Files, "$Arg") ;
- $InputFile = $Arg ;
- }
- elsif ($Arg =~ /^-O.+/)
- {
- $Arg =~ s/^-O// ;
- push (@Files, "$Arg") ;
- $OutputFile = $Arg ;
- }
- elsif ($Arg =~ /^-/) {
- print STDERR "\n\aUnrecognized option, '$Arg'$Usage" ;
- &WaitAndExit ("1", "$InterActive") ;
- }
- }
-
-
- if ("$InputFile" eq '')
- {
- print STDERR "$Usage" ;
- &WaitAndExit ("1", "$InterActive") ;
- }
-
- if ("$OutputFile" eq '')
- {
- print STDERR "$Usage" ;
- &WaitAndExit ("1", "$InterActive") ;
- }
-
- if (! @Files)
- {
- print STDERR "$Usage" ;
- &WaitAndExit ("1", "$InterActive") ;
- }
-
-
-
- if (! $DraftVersion)
- { print "\nPreparing buildable source SGML for FINAL version...\n" ; }
- else
- { print "\nPreparing buildable source SGML for DRAFT version...\n" ; }
-
-
- ## DRD Need to add a check previous to this in the process to
- ## check for use of file entities within <comment> elements.
- ## Writers aren't supposed to use file entities in <comment>
- ## elements because it is too easy to have a big chunk of text
- ## "disappear" from final output. This check needs to be done
- ## on the component SGML files before the buildable SGML source
- ## is made (spam resolves the references).
-
-
- ## DRD work out how to do this without so many temporary files later....
-
- ### Clean up the SGML instance:
- print "\n Preparing SGML instance for processing..." ;
- if (! open (FileI, "$InputFile")) {
- print STDERR "\a\n$Name: $InputFile\"\n" ;
- &WaitAndExit ("16", "$InterActive") ;
- }
- undef $/ ;
- $Buff = <FileI> ;
- close (FileI) ;
- $/ = "\n" ;
- $FileName = "$InputFile" ;
- &CleanUp (\$FileName, \$Buff) ;
-
- if (! (open (FileZ, "> $TmpFile"))) {
- print STDERR "\a\n$Name: $TmpFile\n" ;
- &WaitAndExit ("13", "$InterActive") ;
- }
- print FileZ join ('', $Buff) ;
- close (FileZ) ;
- print "..done.\n" ;
-
-
- ### Make public version of file if not making draft version.
- ## Replace TmpFile with public version.
- if (! $DraftVersion)
- {
- $ErrorCode = system ("$XformSGML_public -I$TmpFile -O$TmpFile.2") ;
- if (! $ErrorCode)
- {
- system ("rm -f $TmpFile") ;
- system ("mv $TmpFile.2 $TmpFile") ;
- }
- }
-
-
- ### Build the writer's (back-of-book) index:
- ## (This is specific for Insight.)
- print "\n Processing writer's index...\n" ;
- &RunIt ("$Indexer -i $TmpFile -o $TmpFile.idx") ;
- print " writer's index generated.\n" ;
-
- ### Put generated index into SGML instance:
- print "\n Integrating writer's index..." ;
-
-
- ## Get the book info from the SGML instance:
- if (! open (FileA, "$TmpFile")) {
- print STDERR "\a\n$Name: $TmpFile\"\n" ;
- &WaitAndExit ("16", "$InterActive") ;
- }
- undef $/ ;
- @BuffSGML = <FileA> ;
- close (FileA) ;
- $/ = "\n" ;
- &GetBookInfo (\$BookName, \$PartNo, \$Version, \$Title,
- \$ShortTitle, \$BookShelf, \@BuffSGML) ;
-
- ### Compare shorttitle from SGML with value from Makefile:
- ($InputShortTitle = $OutputFile) =~ s(.*).sgml\1 ;
-
- if ("$InputShortTitle" ne "$ShortTitle")
- {
- print "\n\nCAUTION! The short title in the Makefile is different" ;
- print "\nthan the value in the SGML file." ;
- print "\n Short title from Makefile = $InputShortTitle " ;
- print "\n Short title from SGML = $ShortTitle " ;
- print "\nContinuing, but using value from Makefile.\n\n" ;
- }
-
-
-
- ### Open the "indexless" SGML instance:
- if (! open (FileS, "$TmpFile")) {
- print STDERR "\a\n$Name: $TmpFile.2\"\n" ;
- &WaitAndExit ("16", "$InterActive") ;
- }
- undef $/ ;
- $Buff1 = <FileS> ;
- close (FileS) ;
- $/ = "\n" ;
-
- ## Open the index file:
- if (! open (FileB, "$TmpFile.idx")) {
- print STDERR "\a\n$Name: $TmpFile.idx\"\n" ;
- &WaitAndExit ("16", "$InterActive") ;
- }
- undef $/ ;
- $Buff2 = <FileB> ;
- close (FileB) ;
- $/ = "\n" ;
-
- ## Open the final shorttitle.sgml output file:
- if (! (open (FileZ, "> $OutputFile"))) {
- print STDERR "\a\n$Name: $Outputfile\n" ;
- &WaitAndExit ("13", "$InterActive") ;
- }
-
- ### Insert the index into the SGML file:
- $Buff1 =~ s<index></index>$Buff2gi ;
-
- ### Change shorttitle attribute value to Makefile value:
- ## Might have <partnumber shorttitle="xyz" bookshelf="sgi-admin">
- ## or <partnumber bookshelf="sgi-admin" shorttitle="xyz">
- $Buff1 =~ s<partnumber([^>]*)shorttitle\s*=\s*\"([^"]*)\"<partnumber$1shorttitle=\"$InputShortTitle\"ig ;
-
- # add the NUMHEADS attribute to <sgidocbk> if needed
- if($NumberedHeads == 1) {
- $Buff1 =~ s/(<sgidocbk)/\1 NUMHEADS="Y"/i;
- }
-
- print FileZ join ('', $Buff1) ;
- close (FileZ) ;
- print "..done.\n" ;
-
- ### Remove temporary files:
- unlink "$TmpFile" ;
- unlink "$TmpFile.idx" ;
-
- print "\n...buildable source SGML done.\n\n" ;
-
-
- &WaitAndExit ("0", "$InterActive") ;
-